﻿//DDS = Drop Down Selector 下拉选项
var DDS = {
    "srcObj":null,
    "obj":null,
    "toggle":function(event){
    
        event = (event == null)?window.event:event;
        this.srcObj= event.target?event.target:event.srcElement;

        var ddsDiv = $("#dds");
        if(ddsDiv.length == 0){
            DDS.showDDS();
        }
        ddsDiv.toggle();
        if(ddsDiv.css("display") == "none"){
            DDS.hideDDS();
        }
            
    },
    "showDDS":function(){
        DDS.obj = window.event.srcElement;
        var ddsHTML = "<div id=\"dds\" style=\"width:100px;height:140px;border:solid 1px orange;position:absolute;";
        ddsHTML +="overflow-x:hidden;overflow-y:auto;background-color:#fff;\"></div>";
        $('body').append(ddsHTML);
    
	    var t = DDS.obj.offsetTop,  h = DDS.obj.clientHeight, l = DDS.obj.offsetLeft, p = DDS.obj.type, eh =DDS.obj.style.height ;
	    var st = 0, sl = 0;

        var target = DDS.obj;
	    while (target= target.offsetParent)
	    {
		    st = target.scrollTop;
		    sl = target.scrollLeft;
		    t += target.offsetTop - st;
		    l += target.offsetLeft - sl;
	    }
	    t += st + h;
	    l += sl;
	    
	    $("#dds").css("top",t + "px");
	    $("#dds").css("left",l + "px");
	    DDS.inserContent();
	    //$("#dds").focus();
    },
    
    "hideDDS":function(){
        $("body").append($("#places"));
        $("body").append($("#categories"));
        $("body").append($("#people"));
        $("body").append($("#tags"));
        $("#places").css("display","none");
        $("#categories").css("display","none");
        $("#people").css("display","none");
        $("#tags").css("display","none");
        $("#dds").remove();
    },
    
    "inserContent":function(){
        var ddsdiv = document.getElementById("dds");
        switch(DDS.obj.name)
        {
            case "startattime":
                DDS.insertStartAtTime();
                break;
            case "endattime":
                DDS.insertEndAtTime();
                break;
            case "SpendAtTime":
                DDS.insertStartAtTime();
                break;
           case "Place":
                DDS.insertMoneySpanPlace();
                break;
           case "People":
                DDS.insertTimeSpanPerson();
                break;
           case "tags":
               
                DDS.insertTimeSpanTag();
                break;
           case "CategoryName":
                DDS.insertMoneySpanCategories();
                break;
        }
    },
    
    "setValue":function(ele){
        DDS.obj.value = ele.innerHTML;
        DDS.hideDDS();
        DDS.obj.focus();
    },
    
    "insertStartAtTime":function(){
        var div = "<div style=\"line-height:20px;padding:0 5px 0 5px;white-space:nowrap;\" onclick=\"DDS.setValue(this);\"  ";
        div += " onmouseover=\"this.style.backgroundColor='#999';\" onmouseout=\"this.style.backgroundColor='#fff';\" >";
        for(var i=0;i<24;i++)
        {
            $("#dds").append(div + (i <10?("0" + i):i) + ":00" + "</div>");
            $("#dds").append(div + (i <10?("0" + i):i) + ":30" + "</div>");
        }
    },
    
     "insertEndAtTime":function(){
        var div = "<div style=\"line-height:20px;padding:0 5px 0 5px;white-space:nowrap;\" onclick=\"DDS.setValue(this);\"  ";
        div += " onmouseover=\"this.style.backgroundColor='#999';\" onmouseout=\"this.style.backgroundColor='#fff';\" >";
        for(var i=0;i<24;i++)
        {
            $("#dds").append(div + (i <10?("0" + i):i) + ":00" + "</div>");
            $("#dds").append(div + (i <10?("0" + i):i) + ":30" + "</div>");
        }
    },
    
    "insertMoneySpanPlace":function(){
        $("#dds").css("width","250px");
        $("#dds").append($("#places"));
        $("#places").css("display","block");
    },
    
    "insertMoneySpanCategories":function(){
        $("#dds").css("width","250px");
        $("#dds").append($("#categories"));
        $("#categories").css("display","block");
    },
    
    
    "insertTimeSpanPerson":function(){
        $("#dds").css("width","250px");
        $("#dds").append($("#people"));
        $("#people").css("display","block");
    },
    
     "insertTimeSpanTag":function(){
        $("#dds").css("width","250px");
        $("#dds").append($("#tags"));
        $("#tags").css("display","block");
    }
}